package ru.sgu.codecenter.fidx;

import ru.sgu.codecenter.fidx.command.*;
import ru.sgu.codecenter.fidx.model.Engine;
import ru.sgu.codecenter.fidx.model.LuceneEngine;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

public class Main {
    public static void main(String[] args) {
        System.out.println("FIDX by Saratov State University Programming Competitions Training Center\n");
        long startTime = System.currentTimeMillis();

        Engine engine = new LuceneEngine(Configuration.getStorageDir());

        List<Command> commands = new ArrayList<Command>();
        commands.add(new DeleteCommand());
        commands.add(new UpdateCommand());
        commands.add(new RescanCommand());
        commands.add(new ShowCommand());
        commands.add(new FindCommand());
        commands.add(new HelpCommand());

        for(Command command : commands)
            command.setEngine(engine);

        if (args.length == 0) {
            System.out.println("Usage:\tfidx <command> [<command-arg> ... <command-arg>]");
            System.out.println("Help:\tfidx help");
            return;
        }

        try {
            boolean commandFound = false;
            String commandName = args[0];
            for (Command command : commands) {
                if (command.getName().equals(commandName)) {
                    String[] commandArguments = new String[args.length - 1];
                    System.arraycopy(args, 1, commandArguments, 0, commandArguments.length);
                    command.execute(commandArguments);
                    commandFound = true;
                }
            }

            if (!commandFound) {
                Command.error("Incorrect command.");
            }
        } catch (OperationFailedException e) {
            Command.error(e.getMessage());
        }

        long millis = System.currentTimeMillis() - startTime;

        long days = TimeUnit.MILLISECONDS.toDays(millis);
        millis -= TimeUnit.DAYS.toMillis(days);

        long hours = TimeUnit.MILLISECONDS.toHours(millis);
        millis -= TimeUnit.HOURS.toMillis(hours);

        long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
        millis -= TimeUnit.MINUTES.toMillis(minutes);

        long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
        millis -= TimeUnit.SECONDS.toMillis(seconds);

        System.out.print("\nDone in");
        if (days != 0) {
            System.out.print(" " + days + " days");
        }

        if (hours != 0) {
            System.out.print(" " + hours + " hours");
        }

        if (minutes != 0) {
            System.out.print(" " + minutes + " min");
        }

        if (seconds != 0) {
            System.out.print(" " + seconds + " sec");
        }

        if (millis != 0) {
            System.out.print(" " + millis + " ms");
        }

        System.out.println(".");
    }
}
